package com.example.guardian.personalcenter;

import static android.app.Notification.EXTRA_CHANNEL_ID;
import static android.provider.Settings.EXTRA_APP_PACKAGE;
import static android.widget.Toast.LENGTH_SHORT;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationManagerCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.example.guardian.MainActivity;
import com.example.guardian.R;

public class SystemSeting extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_center_system_settings);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        Button back_button = findViewById(R.id.system_settings_back_btn);
        back_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SystemSeting.this.finish();
            }
        });

        Switch switch_auto = findViewById(R.id.auto_push);
        boolean statea = (ActivityCompat.checkSelfPermission(SystemSeting.this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED);
        switch_auto.setChecked(statea);
        switch_auto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    PERMISSIONS_MICRO();
                }else {
                    Toast.makeText(SystemSeting.this,"关闭该权限请至手机设置", LENGTH_SHORT).show();
                    compoundButton.setChecked(true);
                }
            }
        });


        Switch switch_not = findViewById(R.id.not_push);
        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        switch_not.setChecked(manager.areNotificationsEnabled());
        switch_not.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b = true) {
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                    //这种方案适用于 API 26, 即8.0（含8.0）以上可以用
                    intent.putExtra(EXTRA_APP_PACKAGE, getPackageName());
                    intent.putExtra(EXTRA_CHANNEL_ID, getApplicationInfo().uid);
                    //这种方案适用于 API21——25，即 5.0——7.1 之间的版本可以使用
                    intent.putExtra("app_package", getPackageName());
                    intent.putExtra("app_uid", getApplicationInfo().uid);
                    startActivity(intent);
                }
            }
        });

        Switch switch_lock = findViewById(R.id.LOCK_PUSH);
        boolean statel = (ActivityCompat.checkSelfPermission(SystemSeting.this, Manifest.permission.WAKE_LOCK) == PackageManager.PERMISSION_GRANTED);
        switch_lock.setChecked(statel);
        switch_lock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    PERMISSIONS_LOCK();
                }else {
                    Toast.makeText(SystemSeting.this,"关闭该权限请至手机设置", LENGTH_SHORT).show();
                    compoundButton.setChecked(true);
                }
            }
        });

        Switch switch_camera = findViewById(R.id.CAMERA_push);
        boolean statec = (ActivityCompat.checkSelfPermission(SystemSeting.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
        switch_camera.setChecked(statec);
        switch_camera.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    PERMISSIONS_CAM();
                }else {
                    Toast.makeText(SystemSeting.this,"关闭该权限请至手机设置", LENGTH_SHORT).show();
                    compoundButton.setChecked(true);
                }
            }
        });

        Switch switch_photo = findViewById(R.id.photo_push);
        boolean statep = (ActivityCompat.checkSelfPermission(SystemSeting.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
        switch_photo.setChecked(statep);
        switch_photo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    PERMISSIONS_photos();
                }else {
                    Toast.makeText(SystemSeting.this,"关闭该权限请至手机设置", LENGTH_SHORT).show();
                    compoundButton.setChecked(true);
                }
            }
        });

    }


    private final String[] PERMISSIONS = {   //app需要获取的权限
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA,
            Manifest.permission.WAKE_LOCK};


    private final int REQUEST_GET_IMAGE = 1;//定义相册权限请求码是 1
    private final int REQUEST_GET_CAMERA = 1002;
    private final int REQUEST_GET_MICRO = 1003;
    private final int REQUEST_GET_LOCK = 1004;
    private final int REQUEST_GET_NOT = 1005;


    private void PERMISSIONS_photos() {
        //检测是否有相册的访问权限
        //有 打开并跳转到相册
        if (ActivityCompat.checkSelfPermission(SystemSeting.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            //跳转到相册
            //TODO
        } else { //没有 申请权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_GET_IMAGE);
            //ActivityCompat.requestPermissions(SystemSeting.this, PERMISSIONS, REQUEST_GET_IMAGE);
        }
    }

    private void PERMISSIONS_MICRO() {
        if (ActivityCompat.checkSelfPermission(SystemSeting.this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
            //TODO
        } else { //没有 申请权限
            ActivityCompat.requestPermissions(SystemSeting.this, PERMISSIONS, REQUEST_GET_MICRO);
        }
    }

    private void PERMISSIONS_CAM() {
        if (ActivityCompat.checkSelfPermission(SystemSeting.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
            //TODO
        } else { //没有 申请权限
            ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_GET_CAMERA);
        }
    }

    private void PERMISSIONS_LOCK() {
        if (ActivityCompat.checkSelfPermission(SystemSeting.this, Manifest.permission.WAKE_LOCK) == PackageManager.PERMISSION_GRANTED) {
            //TODO
        } else { //没有 申请权限
            ActivityCompat.requestPermissions(SystemSeting.this, PERMISSIONS, REQUEST_GET_LOCK);
        }
    }


}